/*
The MIT License (MIT)

Copyright (c) 2013 Mike Dapiran, Brian May, Richard Pospesel, and Bert Wierenga

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software 
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include "IProcess.h"
#include "tinyxml.h"

#include "../HLSLCPPDeclarations/Texture2DMappings.fx"
#include "../Directors/DX11Director.h"
#include <d3d11.h>

namespace Hogshead
{
	namespace DX11Rendering
	{
		IProcess::IProcess()
		{
			
		}

		IProcess::~IProcess()
		{
		}

		void IProcess::setParentDirector( DX11Director* in_parent )
		{
			_parent = in_parent;
		}

		void IProcess::registerComponent(IComponent* in_component)
		{
		}

		void IProcess::unregisterComponent(IComponent* in_component)
		{
		}

		void IProcess::update( const GameTimer& in_gt )
		{
		}

		IProcess* IProcess::readXML( const String& in_xml )
		{
			//parse the String for what we need
			TiXmlDocument document;
			document.Parse(in_xml);

			TiXmlElement* root_element = document.RootElement();
			String root_tag(root_element->Value());

			

			if(root_tag.equals("Process"))
			{
				_handle = -1;
				root_element->QueryIntAttribute("handle", &_handle);				

				if( _handle == -1 )
					error("process needs to have a handle assigned to it");

				//parse children
				int reg;
				String name;
				int usage;
				bool clear = false;
				Color clear_color;
				String flags;
				float depth;
				char stencil;

				TiXmlNode* child_node = NULL;
				TiXmlElement* child_element;

				bool cull_read;
				if(TIXML_NO_ATTRIBUTE != root_element->QueryBoolAttribute("culling", &cull_read))
					_culling = cull_read;
				else
					_culling = true;


				while(child_node = root_element->IterateChildren(child_node))
				{
					child_element = child_node->ToElement();
					if(!child_element)
						continue;
					
					TEXTURE_BINDING_INFO info;
					

					String tag(child_element->Value());
					if(tag.equals("RenderTargetView"))
					{						
						if( child_element->QueryIntAttribute("register", &reg) == TIXML_NO_ATTRIBUTE )
							reg = -1;

						if( child_element->QueryColorAttribute("clear_color", &clear_color) == TIXML_NO_ATTRIBUTE )
						{
							clear = false;
						}
						else
						{
							clear = true;
							for( int i = 0; i < 4; i++ )
							{
								info.clear_data.color[i] = ((float*)clear_color)[i];
							}
						}

						child_element->QueryStringAttribute("name", &name);
						usage = RENDER_TARGET_VIEW;
					}
					else if(tag.equals("ShaderResourceView"))
					{
						if( child_element->QueryIntAttribute("register", &reg) == TIXML_NO_ATTRIBUTE )
							error("You must specify a register to send the ShaderResource to");

						if ( reg < MAP_COUNT )
							error( StringBuilder() << "The system reserves texture registers less than " << MAP_COUNT << ". Use a higher register." );

						child_element->QueryStringAttribute("name", &name);
						usage = SHADER_RESOURCE_VIEW;
					}
					else if(tag.equals("DepthStencilView"))
					{
						child_element->QueryStringAttribute("name", &name);
						usage = DEPTH_STENCIL_VIEW;

						if( child_element->QueryStringAttribute("flags", &flags) == TIXML_NO_ATTRIBUTE )
						{
							clear = false;
						}
						else
						{
							clear = true;
							child_element->QueryFloatAttribute("depth", &depth);
							child_element->QueryByteAttribute("stencil", &stencil);

							info.clear_data.depth_stencil.depth = depth;
							info.clear_data.depth_stencil.stencil = stencil;

							UINT flag = 0;
							if( flags.equals("ds") || flags.equals("s") || flags.equals("sd"))
								flag |= D3D10_CLEAR_STENCIL;
							if( flags.equals("ds") || flags.equals("d") || flags.equals("sd"))
								flag |= D3D10_CLEAR_DEPTH;

							info.clear_data.depth_stencil.flags = flag;
						}
					}

					
					info.name = name;
					info.send_to_register = reg;
					info.usage = (RenderingBufferUsage)usage;
					info.clear = clear;

					_texture_buffer_needs.add( info );
				}

				
			}

			return this;
		}

		Vector<TEXTURE_BINDING_INFO>& IProcess::getTextureRequests()
		{
			return _texture_buffer_needs;
		}

		bool IProcess::enabled() const
		{
			return _enabled;
		}

		int IProcess::handle() const
		{
			return _handle;
		}
	}
}
